﻿using System;
using System.IO;
using System.Xml.Linq;
using System.Xml.Schema;

namespace Automator.Runtime.Classes
{
    /// <summary>
    /// Static class that provides routines for validation of Automator XML files like preparing XSD schemas and
    /// constants for XML namespaces and actual file format version
    /// </summary>
    public static class XmlValidation
    {
        public static readonly XNamespace SolutionNamespace = "http://automator.codeplex.com/solution";
        public static readonly XNamespace WorkflowNamespace = "http://automator.codeplex.com/workflow";
        public static readonly string CurrentFormatVersion = "1.01";
        
        public static XmlSchemaSet SolutionSchema { get; set; }
        public static XmlSchemaSet WorkflowSchema { get; set; }
        
        /// <summary>
        /// Loads XSD schemas for solution and workflow files
        /// </summary>
        static XmlValidation()
        {
            string xsdPath;
            xsdPath = Path.Combine(Automator.Runtime.Environment.schemasDir, "Solution.xsd");
            if (File.Exists(xsdPath))
            {
                SolutionSchema = new XmlSchemaSet();                
                SolutionSchema.Add(SolutionNamespace.NamespaceName, xsdPath);
            }
                        
            xsdPath = Path.Combine(Automator.Runtime.Environment.schemasDir, "Workflow.xsd");
            if (File.Exists(xsdPath))
            {
                WorkflowSchema = new XmlSchemaSet();
                WorkflowSchema.Add(WorkflowNamespace.NamespaceName, xsdPath);
            }
        }
    }
}
